#!/usr/bin/env python

import sys
import re

AGENT = 8

red = 0
blue = 1
yellow = 2
pink = 3
purple = 4
beige = 5
lightblue = 6
green = 7

def main():

   if len(sys.argv) >= 3:

      matches = []
      colors = []
      i = 0
      for match in sys.argv[2:]:
         matches.append(match)
         colors.append(i)
         i = (i + 1) & 7

      f = open(sys.argv[1])

      # find highest agent id
      highestagentid = 0
      for line in f:
         if line.startswith('NAM') and int(line.split(' ')[1]) == AGENT:
            if int(line.split(' ')[2]) > highestagentid:
               highestagentid = int(line.split(' ')[2])
      if highestagentid == 0:
         highestagentid = AGENT * 1000

      #sys.stderr.write('highest agent id is:%d\n' % highestagentid)
      nextagentid = highestagentid + 1

      f.seek(0)

      # pass 1

      nam = False;
      last_tstamp = 0;
      first = True;
      nams = []

      agentnams = []

      for line in f:

         if line.startswith('OCC') or line.startswith('STA') or line.startswith('STO'):
            last_tstamp = line.split()[3]

         if line.startswith('OCC') or line.startswith('STA'):

            line = f.next()
            dscwords = line.split()

            i = 0
            for match in matches:

               #sys.stderr.write('match is:%s\n' % match)

               if re.compile(match).findall(dscwords[3]):

                  #sys.stderr.write('matched:%s\n' % dscwords[3])

                  if not match in nams:
                     agentnams.append("NAM 8 %d %s" % ( (nextagentid + i), match))
                     nams.append(match)

               i = i + 1


      f.seek(0)


      # pass 2
      nam = False;
      last_tstamp = 0;
      first = True;
      nams = []
      printagentnams = True

      for line in f:

         if printagentnams:
            if line.startswith('NAM'):

               for match in matches:
                  for agentnam in agentnams:
                     if match == agentnam.split()[3]:
                        print agentnam

               printagentnams = False

         print line,


         if line.startswith('OCC') or line.startswith('STA'):
            last_tstamp = line.split()[3]
            #sys.stderr.write('line: %s %s\n' % (last_tstamp, line))

         if line.startswith('OCC') or (line.startswith('STA') and not line.startswith('STA 3')):

            line = f.next()
            print line,

            if line.startswith('DSC'):

               #sys.stderr.write('line is: %s %s\n' % (last_tstamp, line))

               dscwords = line.split()
               
               i = 0
               for match in matches:

                  #sys.stderr.write('match is: %d %s %s\n' % (i, match, last_tstamp))

                  result = re.compile(match).findall(dscwords[3])
                  if result:

                     #sys.stderr.write('%s dscwords is:%s\n' % (last_tstamp, dscwords[3]))

                     if first:
                        first = False
                        print 'STA 8 %d' % (nextagentid + i), last_tstamp
                        print 'DSC 3 0', 0 + colors[i]

                     else:
                        print 'STO 8 %d' % (nextagentid + i), last_tstamp

                        colors[i] = colors[i] ^ 7

                        print 'STA 8 %d' % (nextagentid + i), last_tstamp
                        print 'DSC 3 0', 0 + colors[i]

                  i = i + 1

      f.close()

   else:
      print("Usage: %s tdifile pattern1 [pattern2]..." % sys.argv[0])

if __name__ == "__main__":
   main()